<?php
/**
 * File htdocs/Aplication/Models/updateque.php ini berisi class Updateque
 *
 * PHP version 5.3
 *
 * @category Models
 * @package  Htdocs/Aplication/Models
 * @author   dio ratar <dio.22ratar@gmail.com>
 * @license  FamilyTree
 * (http://en.wikipedia.org/wiki/Proprietary_software)
 * @link     http://www.FamilyTree.dio22r.com/
 */

if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
 * pengisian form :
 * tambah :
 * 1. pilih mau nambah anak ato pasangan...
 * 2. pilih pasangan/anaknya siapa....
 * 3. isi form...
 * 4. upload foto... boleh di skip...
 * 5. view bio...
 * 6. close...
 * 
 *  
 *  edit :
 *  1. lansung klik di nama
 *  2. tampil view bio...
 *  3. edit or close...
 *  4. edit : isi form...
 *  5 view bio...
 */

/**
 * Class updateque merupakan class yang berisi perintah-perintah SQL 
 * yang digunakan untuk update.
 *
 * @category Models
 * @package  Htdocs/Aplication/Models
 * @author   dio ratar <dio.22ratar@gmail.com>
 * @license  FamilyTree
 * (http://en.wikipedia.org/wiki/Proprietary_software)
 * @link     http://www.FamilyTree.dio22r.com/
 */
class biodata_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper("form");
		$this->load->helper("url");
		$this->load->model('db_access');
	}
	
	
    public function retrieve_all()
    {
    	$result = $this->db->get_where("data_diri");
    	
    	$arrRes = $this->db_access->to_array($result);
    	
    	return $arrRes;
    }
    
    public function retrieve_couple()
    {
    	$sql = "SELECT t2.id_nikah, t1.nama as pria, t3.nama as wanita
				FROM data_diri t1
				JOIN nikah t2 ON t2.pria = t1.id_data_diri
				JOIN data_diri t3 ON t2.wanita = t3.id_data_diri";
    	
    	$result = $this->db->query($sql);
    	
    	$arrRes = $this->db_access->to_array($result);
    	
    	return $arrRes;
    }
    
    public function insert_bio(
    	$id, $name, $poBirth, $doBirth, $sex, $type
    ) {
    	
    	$arrData = array (
    		'id_data_diri' => '',
    		'nama' => $name,
    		'tempat_lahir' => $poBirth,
    		'tanggal_lahir' => $doBirth,
    		'jenis_kelamin' => $sex,
    		'tipe' => ""
    	);
    	
    	if ($type == "pasangan") {
    		$memberId = $id; 
    		$arrData['tipe'] = "branch"; 
    	} else {
    		$coupleId = $id;
    		$arrData['tipe'] = "trunk";
    	}
    	
    	$strSql = $this->db->insert_string("data_diri", $arrData);
    	$this->db->query($strSql);
    	
    	$newMemberId = $this->db->insert_id();
    	
    	if ($type == "pasangan") {
    		$this->insert_couple($memberId, $newMemberId);
    	} else {
    		$this->insert_child($coupleId, $newMemberId);
    	}
    	
    	echo $this->db->insert_id();
    }
    
    private function insert_child($coupleId, $newMemberId)
    {
    	$arrData = array (
    		'id_anak' => '',
    		'id_nikah' => $coupleId,
    		'id_data_diri' => $newMemberId
    	);
    	
    	$strSql = $this->db->insert_string("anak", $arrData);
    	
    	$this->db->query($strSql);
    }
    
    private function insert_couple($memberId, $newMemberId)
    {
    	$arrSelect = array (
    		'id_data_diri' => $memberId
    	);
    	
    	$resMember = $this->general_select("data_diri", $arrSelect);
    	
    	if (count($resMember) > 0) {
	    	$memberSex = $resMember[0]['jenis_kelamin'];
	    	
	    	$arrInsert = array (
	    		'id_nikah' => '',
	    		'Pria' => '',
	    		'Wanita' => ''
	    	);
	    	
	    	if ($memberSex == 'L') {
	    		$arrInsert['Pria'] = $memberId;
	    		$arrInsert['Wanita'] = $newMemberId;
	    	} else {
				$arrInsert['Pria'] = $newMemberId;
	    		$arrInsert['Wanita'] = $memberId;
	    	}
	    	
	    	$strSql = $this->db->insert_string("nikah", $arrInsert);
	    	echo $strSql;
	    	$this->db->query($strSql);
    	}
    }
    
    public function select_child($id)
    {
        $parentId = $this->db->escape($parentId);
        $query = "SELECT t1.* FROM data_diri t1, anak t2, nikah t3
                    WHERE (t3.Pria = '".$parentId."' OR t3.Wanita = '".$parentId."')
                    AND t3.id_nikah = t2.id_nikah
                    AND t2.id_data_diri = t1.id_data_diri";
        
        $result = $this->db->query($query);
        $arrRes = $this->db_access->to_array($result);
        
        return $arrRes;
    }
    
	public function general_select($table, $arrData)
    {
    	$arrWhere = array ();
		foreach ($arrData as $key => $value) {
			$value = $this->db->escape($value);
			$arrWhere[] = $key . " = " . $value;
		}
		
		$strWhere = implode(" AND ", $arrWhere);
		
		$strSql = "SELECT * FROM " . $table
				. " WHERE " .$strWhere;
				
		$result = $this->db->query($strSql);
		
		$arrRes = $this->db_access->to_array($result);
		
		return $arrRes;
    }
}